<template>
	<view class="layer">
		<div v-if="isLogin" class="content-box">
			<div class="logo-box">
				<image src="../../static/logo.png" alt="" class="logo" style="width: 250rpx;height: 250rpx;"></image>
				<text class="title">护家Lane智辅系统</text>
			</div>
			<u--form labelPosition="left" labelWidth="50px" :model="userInfo" :rules="rules">
				<u-form-item label="用户名" prop="username">
					<u--input placeholder="请输入用户名" border="bottom" v-model="userInfo.username" clearable></u--input>
				</u-form-item>
				<u-form-item label="密码" prop="password">
					<u--input placeholder="请输入密码" border="bottom" v-model="userInfo.password" clearable type='password'
						:password='true'></u--input>
				</u-form-item>
			</u--form>
			<div class="botton-group">
				<u-button icon="checkbox-mark" type="primary" text="登录" style="width: 40%;" @click="login"></u-button>
				<u-button icon="attach" type="warning" text="去注册" style="width: 40%;" @click="ToRegisterPage"></u-button>
			</div>
		</div>
		<div v-else style="content-box">
			<div class="logo-box">
				<image src="../../static/logo.png" alt="" class="logo" style="width: 250rpx;height: 250rpx;"></image>
				<text class="title">护家Lane智辅系统</text>
			</div>
			<u--form labelPosition="left" labelWidth="50px" :model="userInfo" :rules="rules">
				<u-form-item label="用户名" prop="username">
					<u--input placeholder="请输入用户名" border="bottom" v-model="userInfo.username" clearable></u--input>
				</u-form-item>
				<u-form-item label="密码" prop="password">
					<u--input placeholder="请输入密码" border="bottom" v-model="userInfo.password" clearable type='password'
						:password='true'></u--input>
				</u-form-item>
			</u--form>
			<div class="botton-group">
				<u-button icon="attach" type="warning" text="去登录" style="width: 40%;" @click="ToLoginPage"></u-button>
				<u-button icon="checkbox-mark" type="primary" text="注册" style="width: 40%;" @click="register"></u-button>
			</div>
		</div>
		<u-notify ref="uNotify" message="Hi uView"></u-notify>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 显示登陆页面还是注册页面
				isLogin: true,
				userInfo: {
					username: 'admin',
					password: 'admin',
				},
				rules: {
					username: {
						type: 'string',
						required: true,
						message: '请填写用户名',
						trigger: ['blur', 'change']
					},
					password: [{
						required: true,
						min: 3,
						max: 12,
						message: '请填写3~12位密码',
						trigger: ['blur'],
					}],
				},
			}
		},
		onLoad() {

		},
		methods: {
			ToRegisterPage() {
				this.isLogin = false;
				this.userInfo.username = '';
				this.userInfo.password = '';
			},
			ToLoginPage() {
				this.isLogin = false;
			},
			register() {
				let that = this;
				uni.request({
					url: 'http://yiyuy.nat300.top/user/add',
					method: 'POST',
					data: that.userInfo,
					success(res) {
						console.log(res)
						if (res.data.code === 200) {
							uni.setStorageSync('username', that.userInfo.username)
							that.$refs.uNotify.show({
								type: 'success',
								message: '注册成功',
								duration: 1000 * 3,
							})
							// 跳转到主页面
							uni.switchTab({
								url: '/pages/main/main'
							})
						} else {
							that.$refs.uNotify.show({
								type: 'error',
								message: '注册失败',
								duration: 1000 * 3,
							})
						}
					},
					fail(err) {
						console.log(err)
					}
				})
			},
			login() {
				let that = this;
				uni.request({
					url: 'http://yiyuy.nat300.top/user/login',
					method: 'POST',
					data: that.userInfo,
					success(res) {
						if (res.data.code === 200) {
							uni.setStorageSync('username', that.userInfo.username)
							that.$refs.uNotify.show({
								type: 'success',
								message: '登陆成功',
								duration: 1000 * 3,
							})
							// 跳转到主页面
							uni.switchTab({
								url: '/pages/main/main'
							})
						} else {
							that.$refs.uNotify.show({
								type: 'error',
								message: '登陆失败',
								duration: 1000 * 3,
							})
						}
					},
					fail(err) {
						console.log(err)
					}
				})
			}
		}
	}
</script>

<style>

	.layer {
		width: 100%;
		height: 100vh;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.content-box {
		margin: 50% auto;
		width: 80%;
		display: flex;
		flex-direction: column;
	}

	.logo-box {
		width: 100%;
		margin-bottom: 5%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		width: 50px;
		height: 50px;
	}

	.title {
		margin-top: 5%;
		font-size: 28px;
		font-weight: bold;
	}

	.botton-group {
		display: flex;
		margin-top: 5%;
	}
</style>